*******************************************************
* Purpose: descriptive tables and figures in paper
*******************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

* Distribution of EPDS in full pop compared to final sample. Full pop: all tested in NOVAX muni between 2012-2017. 
use "$work\mfr_sample",clear

merge 1:1 pnr using "$work\background", keep(3) nogen
merge m:1 mfr_yob mfr_muni using "$work\munics_grupper", keep(3) nogen // munics using grupper
 merge m:1 mfr_yob mfr_muni using "$work\munics_tested", keep(3) nogen keepusing(mfr_yob mfr_muni)
merge 1:1 pnr using "$work\grupper_wide", keep(1 3) nogen // individuals in groups
egen group = group(GRUPPEID1)
merge 1:1 pnr using "$work\efterfodsel_mor.dta", keep(1 3) nogen

gen tested = 0
replace tested = 1 if score_e1 != .
keep if tested == 1
tab score_e1
save "$work\distind_all", replace
gen id = 1
egen total = sum(id)

collapse (sum) id (mean) total, by(score_e1)
gen freq = id/total*100
replace freq = 0 if id < 6
save "$work\distepds_all" , replace


use "$work\final_data1", clear
su score_e1
tab score_e1
save "$work\distind_sample", replace

gen id = 1
egen total = sum(id)

collapse (sum) id (mean) total, by(score_e1)
gen freq = id/total*100
replace freq = 0 if id < 6
save "$work\distepds_sample" , replace

use "$work\distepds_all",clear
gen all = 1
append using  "$work\distepds_sample" 
replace all = 0 if all == .

********************************************************************************
*Figure C1 Distribution of Mental Health Screening Scores
********************************************************************************
twoway (lpoly freq score_e1 if all == 1, fcolor(black) alwidth(none) lcolor(black) lwidth(thick) bwidth(1) kernel(rec) deg(0)) ///
(scatter freq score_e1 if all == 1, msize(small) msymbol(Oh) mcolor(black) alwidth(none) lcolor(black) lwidth(thick)) ///
(lpoly freq score_e1 if all == 0, fcolor(black%20) alwidth(none) lcolor(black%20) lwidth(thick) bwidth(1) kernel(rec) deg(0)) ///
(scatter freq score_e1 if all == 0, msize(small) msymbol(Oh) mcolor(black%20) alwidth(none) lcolor(black) lwidth(thick)) ///
, $gs xtitle("Mental health screening score") ytitle("Share, %") ylab(0(5)20, tlcolor(gs10) labgap(6-pt) format(%4.0f)) legend(order(3 1) label(1 "Full population") label(3 "Analysis sample"))
graph export "$out\distribtion_epds1.png", replace

use "$work\distind_sample",clear
gen all = 0
append using "$work\distind_all"
replace all = 1 if all == .

ksmirnov score_e1, by(all) 


********************************************************************************
* Table C2: Summary Statistics: Comparison of Groups by EPDS Screening Coverage in the Group
********************************************************************************
use "$work\final_data", clear // in mother groups yob 2012-2017


gen sample = 0
replace sample = 1 if tested == 1 & tested_mean == 1
eststo clear
eststo: estpost summarize $covariates score_e1 if sample == 0
est store a
eststo: estpost summarize $covariates score_e1 if sample == 1
est store b

esttab a b using "$out\descriptives1.tex", fragment nonum mlabels(none) label cells("mean(fmt(%13.2fc))") collabels(\multicolumn{1}{c}{Mean}) nodepvar alignment(c c) nolegend nonote replace

********************************************************************************
* Table 2: Summary Statistics: Characteristics of Treated and Control Individuals
********************************************************************************

use "$work\final_data1", clear 
gen age_t = Dato1 - mfr_dob

merge m:1 group using "$work\nr_nurses" , keep(1 3) nogen
label var nr_nurses "Number of different nurses assigned in group"
label var age_t "Child's age at EPDS Screening, days"
estpost ttest $covariates high score_e1 age_t nr_nurses $mainout, by(peer_treat)

esttab . using "$out\descriptives_new.tex" , cells("mu_1(fmt(3)) mu_2(fmt(3)) p(fmt(3))") replace label noobs
* Note: Headings in table formatted locally: No peers with depression, depressed peer, p-values

********************************************************************************
* Table C1: Summary Statistics: Comparison of Families by Group Participation
********************************************************************************
use "$work\mfr_sample",clear // er det fra fuld mfr sample vi skal starte her?
merge 1:1 pnr using "$work\background", keep(3) nogen
merge m:1 mfr_yob mfr_muni using "$work\munics_grupper", keep(3) nogen // munics using grupper
merge m:1 mfr_yob mfr_muni using "$work\munics_tested", keep(3) nogen
merge 1:1 pnr using "$work\grupper_wide", keep(1 3)  // individuals in groups

gen in_g = 0
replace in_g = 1 if _merge == 3

merge m:1 cprm mfr_dob using "$work\psykkontaktmoms", keep(1 3) nogen
foreach var of varlist  postbirth* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
merge m:1 cprm mfr_dob using "$work\conssssy_m", keep(1 3) nogen
foreach var of varlist gpm_cons* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
merge m:1 cprm mfr_dob using "$work\psyksssy_m", keep(1 3) nogen
foreach var of varlist gpm_psyk* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
merge m:1 cprm mfr_dob using "$work\outmoms_monthly_pre", keep(1 3) nogen // outpat outcomes
foreach var of varlist out* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
merge m:1 cprm mfr_dob using "$work\inmoms_monthly_pre", keep(1 3) nogen // inpat outcomes
foreach var of varlist inp* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
merge m:1 cprm mfr_dob using "$work\conssssy_monthly_pre", keep(1 3) nogen // monthly gp couns mom
foreach var of varlist gpmw_cons* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
merge m:1 cprm mfr_dob using "$work\psyksssy_monthly_pre", keep(1 3) nogen // monthly psyk mom
foreach var of varlist gpmw_psyk* {
replace `var' = 0 if `var' == .	
replace `var' = 1 if `var' > 0
}
label var married "Married"
label var cohab "Cohabiting"
label var dk_f "Danish, father"
label var uni_f "Uni. degree, father"
label var higher_f "Higher educ, father"
label var grund_f "Prim. school, father"
label var inc_f "Income, father"
label var dk_m "Danish, mother"
label var uni_m "Uni. degree, mother"
label var higher_m "Higher educ, mother"
label var grund_m "Prim. school, mother"
label var inc_m "Income, mother"
label var mfr_preterm "Preterm birth"
label var mfr_first "First-time mothers" 
label var mfr_childnight "Hosp. nights at birth, child"
label var mfr_homebirth "Home birth"
label var mfr_lowbw "Low birth weight"
label var mfr_cs "C-section"
label var mfr_female "Child sex"
label var alder_m "Age, mother"
label var alder_f "Age, father"
label var mfr_paritet "Birth order"
label var gpmw_psyk1y_pre "Psychologist/psychiatrist prior 1y to birth"
label var gpmw_cons1y_pre "GP mental health consultation 1y prior to birth"
label var inp1y_pre "Inpatient mental health hosp. 1y prior to birth"
label var out1y_pre "Outpatient mental health hosp. 1y prior to birth"

egen g_members = count(mfr_yob), by(GRUPPEID1)
replace in_g = 0 if g_members < 3 

eststo clear
eststo: estpost summarize $covariates if in_g == 0
est store t
eststo: estpost summarize $covariates if in_g == 1
est store tt

esttab t tt using "$out\descriptives_groups1.tex", fragment nonum mlabels(none) label cells("mean(fmt(%13.2fc))") collabels(\multicolumn{1}{c}{Mean}) nodepvar alignment(c c) nolegend nonote replace



********************************************************************************
* From here: averages/descriptive stats reported in paper text
********************************************************************************


********************************************************************************
* Nurses pr. muni in 2017  - reported in the paper in footnote 25
********************************************************************************
use "$work\mfr_sample",clear

merge 1:1 pnr using "$work\shp_first.dta", keep(3) nogen keepusing(shp_first)

merge m:1 mfr_muni using "$work\novaxmuni", nogen keep (3)
keep if mfr_yob == 2017
egen antal = count(mfr_yob), by(shp_first)
*drop if antal < 20
egen shp = group(shp_first)
collapse (count) mfr_yob , by(mfr_muni shp)
collapse (count) shp , by(mfr_muni)
su shp,detail
gen sd = shp
gen all = 1
egen mean_shp= mean(shp) 
pctile p25_shp = shp, n(4)
keep if _n < 4
keep mean_shp p25
export excel "$out\numberofshp",replace firstrow(variables)

********************************************************************************
* Nurse district stats reported in footnote 25
********************************************************************************
use "$work\final_data1", clear
forvalues i =2012/2017 {
	preserve
	keep if mfr_yob == `i'
	codebook mfr_muni
	codebook fe
	restore
}
gen id = 1
preserve
collapse id, by(fe mfr_muni)
sort mfr_muni fe
bys mfr_muni: egen antal_dis = count(id) 
collapse antal_dis, by(mfr_muni)
su antal_dis, detail // antal dis pr. muni
sort antal_dis
egen mean_antal_dis = mean(antal_dis)
keep if _n == 1 
keep mean_antal_dis
export excel "$out\antal_dis.xls", replace
restore

preserve
collapse id, by(fe shp_first)
bys fe: egen antal_shp = count(id)
collapse antal_shp, by(fe)
su antal_shp, detail // antal shp pr. district
egen mean_antal_shp = mean(antal_shp)
keep if _n == 1 
keep mean_antal_shp
export excel "$out\antal_shp.xls", replace
restore

********************************************************************************
* more on the mother groups
* number of nurses on avg
********************************************************************************
use "$work\final_data", clear 

gen id = 1
collapse (sum) id , by(group nurse)
collapse (sum) id (count) nurse, by(group)
keep group nurse
rename nurse nr_nurses
save "$work\nr_nurses", replace 


********************************************************************************
* Age difference between youngest and oldest child in groups reported in paper
********************************************************************************
use "$work\final_data1", clear
sort group mfr_dob
by group: egen dob_h = min(mfr_dob)
by group: egen dob_l = max(mfr_dob)
gen age_l = date_gruppe - dob_l
replace age_l = 0 if age_l < 0
gen age_h = date_gruppe - dob_h
su age_l
su age_h

gen diff_in_days = age_h - age_l

hist diff_in_days if diff_in_days < 150
sum diff_in_days, d

********************************************************************************
* Age when assignment to mother group
********************************************************************************
use "$work\final_data1", clear
gen age_assignment= date_gruppe - mfr_dob
sum age_assignment, d
